package iu.gq.db.items;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class ChapterItem implements Parcelable {
	private String id, title;
	private int position = 0;

	public ChapterItem() {
		
	}
	
	public ChapterItem(JSONObject in) {
		if (in instanceof JSONObject) {
			try {
				id = in.getString("id");
				title = in.getString("title");
				position = in.getInt("position");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	
	public ChapterItem(Parcel in) {
		id = in.readString();
		title = in.readString();
		position = in.readInt();
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(id);
		dest.writeString(title);
		dest.writeInt(position);
	}
	
	public static final Parcelable.Creator<ChapterItem> CREATOR = new Parcelable.Creator<ChapterItem>() {

		@Override
		public ChapterItem createFromParcel(Parcel source) {
			return new ChapterItem(source);
		}

		@Override
		public ChapterItem[] newArray(int size) {
			return new ChapterItem[size];
		}
	};
	
	public String toJson() {
		JSONObject ob = new JSONObject();
		try {
			ob.put("id", id);
			ob.put("title", title);
			ob.put("position", position);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return ob.toString();
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}
}
